package com.ruoyi.web.controller.safety;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.xuehua.SnowflakeIdUtils;
import com.ruoyi.safety.domain.Area;
import com.ruoyi.safety.dto.AreaDto;
import com.ruoyi.safety.service.IAreaService;
import com.ruoyi.safety.vo.AreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区域Controller
 *
 * @author lth
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/area/area")
public class AreaController extends BaseController
{
    @Autowired
    private IAreaService areaService;

    /**
     * 通过煤矿ID查询区域信息
     */
    @GetMapping("/listById/{mineId}")
    public List listAreaByMineId(@PathVariable String mineId){
        List listByMineId = areaService.listAreaByMineId(mineId);
        return listByMineId;
        //return getDataTable(listByMineId);
    }

    /**
     * 通过区域id获取区域信息
     */
    @GetMapping("/areaById/{areaId}")
    public AreaDto AreaById(@PathVariable String areaId){
        AreaDto areaDto = areaService.AreaById(areaId);
        return areaDto;
        //return getDataTable(listByMineId);
    }

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(Area area)
    {
        startPage();
        List<Area> list = areaService.selectAreaList(area);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('area:area:query')")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") String areaId)
    {
        return success(areaService.selectAreaByAreaId(areaId));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('area:area:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area)
    {
        // 获取区域通过名称
        Area area1 = areaService.getAreaByName(area.getAreaName());
        if (area1 != null){
            return error("该区域已存在，不能再次添加");
        }
        area.setAreaId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('area:area:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area)
    {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('area:area:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{areaId}")
    public AjaxResult remove(@PathVariable String areaId)
    {
        return toAjax(areaService.deleteAreaByAreaId(areaId));
    }
    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:list')")
    @GetMapping("/getList")
    public TableDataInfo getList(AreaVo areaVo)
    {
        startPage();
        List<AreaDto> list = areaService.getAreaList(areaVo);
        return getDataTable(list);
    }
}
